<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{ active: index === activeIndex }"
        @click="changeRole(index)"
      >
        <div class="role-info">
          <svg-icon :icon-class="index === activeIndex ? 'user-active' : 'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <svg-icon icon-class="more" />
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/roleAdd') ">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <!-- title -->
              <div class="tree-title">{{ item.title }}</div>
              <!-- 树形结构 -->
              <el-tree
                ref="tree"
                :props="{label:'title'}"
                :data="item.children"
                show-checkbox
                :default-expand-all="true"
                node-key="id"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="成员列表(100)" name="member">成员管理</el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { getRoleListAPI, getTreeListAPI, getRoleDetailAPI, getRoleUserAPI } from '@/api/system'

export default {
  name: 'Role',
  data() {
    return {
      roleUserList: [], // 当前角色下的成员列表
      roleList: [],
      treeList: [],
      activeIndex: 0, // 记录当前激活的角色项的索引
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      perms: [], // 当前角色权限点列表
      activeName: 'tree' // 默认打开tree
    }
  },
  mounted() {
    this.getRoleList()
    this.getTreeList()
    // 页面加载获取默认选中角色下的权限列表
    this.getRoleDetail(this.roleList[0].roleId)
    // 获取角色成员列表
    this.getRoleUserList(this.roleList[0].roleId)
  },
  methods: {
    async getRoleList() {
      try {
        const res = await getRoleListAPI()
        this.roleList = res.data
        if (this.roleList.length > 0) {
          this.activeIndex = 0 // 默认激活第一个角色
        }
      } catch (error) {
        console.error('Error fetching role list:', error)
        this.$message.error('获取角色列表失败，请稍后再试')
      }
    },
    // 切换角色
    changeRole(index) {
      this.activeIndex = index
      // 注意当前角色下的权限点数据列表
      // 注意角色id 不是索引
      const roleId = this.roleList[index].roleId
      this.getRoleDetail(roleId)
      // 获取当前角色下的成员数据列表
      this.getRoleUserList(roleId)
    },
    async getTreeList() {
      const res = await getTreeListAPI()
      this.treeList = res.data
      // 禁用
      // 目标：treeList里面的每一项以及嵌套的子项都添加一个disabled = true
      this.addDisabled(this.treeList)
    },
    // 递归处理函数
    addDisabled(treeList) {
      treeList.forEach(item => {
        item.disabled = true
        // 递归处理
        if (item.children) {
          this.addDisabled(item.children)
        }
      })
    },
    // 封装请求方法
    async getRoleDetail(roleId) {
      const res = await getRoleDetailAPI(roleId)
      this.perms = res.data.perms
      // 高亮权限列表
      const treeComponentList = this.$refs.tree
      // 调用setCheckedKeys方法
      treeComponentList.forEach((tree, index) => {
        tree.setCheckedKeys(this.perms[index])
      })
    },
    // 封装获取当前角色下的成员的方法
    async getRoleUserList(roleId) {
      const res = await getRoleUserAPI(roleId)
      this.roleUserList = res.data.rows
    }

  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;

  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }

    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;

        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
